package com.future;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.crypt.EncryptionMode;
import org.apache.poi.poifs.crypt.Encryptor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.w3c.dom.DocumentType;

import java.io.*;
import java.security.GeneralSecurityException;

/**
 * JDK版本不支持1.5，版本我看代码是1.8，但是官方说1.6以上，并且这种加密方式，官方说明是2007版本的excel
 *
 * @author future
 * @create 2018-06-01
 */
public class EncryptTest {
    private final static String TEST_WORKBOOK_NAME = "D://test13.xlsx";

    /**
     * TODO 3.17加密方式，资料说是加密2007版本的excel,但是加密xls也是成功，并且wps测试正常，还需在office中测试，存在office读取失败的可能性非常大
     *
     * @param args :
     * @throws IOException
     * @throws GeneralSecurityException
     * @throws InvalidFormatException
     */
    public static void main(String[] args) throws IOException, GeneralSecurityException, InvalidFormatException {
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet1 = wb.createSheet("sheet1");
        XSSFRow row1 = sheet1.createRow(0);
        row1.createCell(0).setCellValue("aaaaa");
        row1.createCell(1).setCellValue("bbbbb");
        row1.createCell(2).setCellValue("ccccc");
        row1.createCell(3).setCellValue("ddddd");
        //把工作薄输出到字节里面
        wb.write(new FileOutputStream(TEST_WORKBOOK_NAME));
        wb.close();

        POIFSFileSystem fs = new POIFSFileSystem();
        final EncryptionInfo info = new EncryptionInfo(EncryptionMode.standard);
        final Encryptor enc = info.getEncryptor();
        //设置密码
        enc.confirmPassword("123456");
        //加密文件
        final OPCPackage opc = OPCPackage.open(new File(TEST_WORKBOOK_NAME), PackageAccess.READ_WRITE);
        final OutputStream os = enc.getDataStream(fs);
        opc.save(os);
        opc.close();

        //把加密后的文件写回到流
        FileOutputStream fos = new FileOutputStream(TEST_WORKBOOK_NAME);
        fs.writeFilesystem(fos);
        fos.close();
    }


    /**excel加密
     * @throws Throwable
     */
    public void encryptExcel(){
        try {
            Workbook wb = new XSSFWorkbook();
            Sheet sheet = wb.createSheet("test1");
            Row row = sheet.createRow(0);
            Cell cell = row.createCell(0);
            cell.setCellValue("test1");
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            wb.write(os);
            ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
            os.reset();
            is.close();
            os.flush();
            os.close();
            System.out.println("End");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}